package com.atguigu.serviceedu.service.impl;

import com.atguigu.dto.MemberDto;
import com.atguigu.exception.Guliexception;
import com.atguigu.serviceedu.entity.UcenterMemberEntity;
import com.atguigu.serviceedu.entity.vo.RegisterVO;
import com.atguigu.serviceedu.mapper.UserCenterMapper;
import com.atguigu.serviceedu.service.UserCenterService;
import com.atguigu.utils.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserCenterServiceImpl extends ServiceImpl<UserCenterMapper, UcenterMemberEntity>  implements UserCenterService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    UserCenterMapper userCenterMapper;
    @Override
    public void registerMember(RegisterVO registerVO) {
        String checkcode = registerVO.getCheckcode();
        String nickname = registerVO.getNickname();
        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        if (StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)
        || StringUtils.isEmpty(checkcode)){
            throw new Guliexception(20001,"昵称-密码-验证码都不能为空");
        }
        if (StringUtils.isEmpty(mobile) || !PhoneNumberUtill.CheckPhoneNumber(mobile)){
            throw new Guliexception(20001,"电话号码格式不正确");
        }
        String o = (String)redisTemplate.opsForValue().get(mobile);
        if (!checkcode.equals(o)){
            throw new Guliexception(20001,"验证码不正确");
        }
        QueryWrapper<UcenterMemberEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count!=0){
            throw new Guliexception(20001,"电话已被注册!");
        }
        UcenterMemberEntity member = new UcenterMemberEntity();
        member.setNickname(nickname);
        member.setIsDisabled(0);
        member.setMobile(mobile);
        member.setPassword(MD5Utils.md5(password));
        member.setAvatar("https://guli-file-zhu.oss-cn-chengdu.aliyuncs.com/avatar/2023/09/06/583989e3-8ac6-47a8-b212-4b45b79ab0c5.jpeg");
        baseMapper.insert(member);
    }

    @Override
    public String memberLogin(LoginVO loginVO) {
        //第一步是校验参数是否合法
        String password = loginVO.getPassword();
        String mobile = loginVO.getMobile();
        if (StringUtils.isEmpty(mobile) || !PhoneNumberUtill.CheckPhoneNumber(mobile)){
            throw new Guliexception(20001,"电话号码格式不正确");
        }
        if (StringUtils.isEmpty(password)){
            throw new Guliexception(20001,"密码不能为空");
        }

        QueryWrapper<UcenterMemberEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        List<UcenterMemberEntity> list = baseMapper.selectList(wrapper);
        UcenterMemberEntity member = list.get(0);
        if (list==null){
            throw new Guliexception(20001,"用户名不存在!");
        }
        if (!loginVO.getPassword().equals(member.getPassword())){
            throw new Guliexception(20001,"用户名或密码错误!");
        }
        if (member.getIsDisabled()==1){
            throw new Guliexception(20001,"用户已被禁用!");
        }
        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setId(member.getId());
        jwtInfo.setNickname(member.getNickname());
        jwtInfo.setAvatar(member.getAvatar());
        String token = JwtUtils.getJwtToken(jwtInfo, 1800);
        try {
            stringRedisTemplate.boundValueOps(token).set(objectMapper.writeValueAsString(list.get(0)));
            stringRedisTemplate.boundValueOps(token).expire(30, TimeUnit.MINUTES);
        }catch (Exception e){
             e.printStackTrace();
        }
        return token;
    }

    @Override
    public UcenterMemberEntity getMemberByTokenInRedis(String token) {
        String strMember = stringRedisTemplate.boundValueOps(token).get();
        if (strMember ==null){
            throw new  Guliexception(20001,"token非法或已过期!");
        }
        UcenterMemberEntity member =null;
        try {
            member = objectMapper.readValue(strMember, UcenterMemberEntity.class);
        }catch (Exception e){
            throw new  Guliexception(20001,"redis中字符串转化对象失败!");
        }
        return member;
    }

    @Override
    public MemberDto queryMemberDtoById(String id) {
        UcenterMemberEntity member = baseMapper.selectById(id);
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(member,memberDto);
        return  memberDto;
    }

    @Override
    public Integer queryRegisterNumForOneDay(String day) {
        Integer num = userCenterMapper.selectRegisterNumForOneDay(day);
        return num;
    }
}
